home *** CD-ROM | disk | FTP | other *** search
- /***************************************************************************
- * *
- * REINDEX.C - Reindex an snews news group article file *
- * *
- * Written by M.S. Muir September 1992 *
- * *
- * *
- * EMAIL: malcolm@muir.demon.co.uk *
- * *
- * Released into the public domain in the hope that someone may find it *
- * to be of use. *
- * *
- * Note however that there is NO WARRANTY WHATSOEVER not even an implied *
- * warranty of merchantability or fitness for any particular purpose. *
- * *
- * This programme may be freely copied and/or modified and/or distributed *
- * under the terms of the GNI General Public Licence, version 1 as *
- * published by the Free Software Foundation. *
- * *
- ***************************************************************************/
-
- /*
- * Source Code Control...
- *
- * $Id: REINDEX.C,v 1.2 1994/02/05 18:48:42 gbj Exp user $
- *
- */
-
- #include "defs.h"
- #include "active.h"
- #include "screen.h"
- #include "reindex.h"
- #ifdef ATARI
- # include "fileops.h"
- # include "st.h"
- #endif
-
- #ifndef __TURBOC__
- #ifndef ATARI
- unsigned long farcoreleft(void);
- #endif
- #endif
-
- INFO my_stuff;
-
- #ifdef ATARI
- extern unsigned long _STACK = 32768;
- #else
- extern unsigned int _stklen = 16384;
-
- #endif
-
- static time_t t;
- static long int first, last;
- static int count, num_art;
-
- void main(int argc, char *argv[])
- {
-
- char newsgroup[81];
- char *fname;
- char *p;
- ACTIVE *gp, *head;
- int ch, junk_flag;
-
- clrscr();
- if (load_stuff()) {
- v_init(my_stuff.directvideo);
- #ifdef ATARI
- textcolor(textf); textbackground(textb);
- #else
- textcolor(7); textbackground(0);
- #endif
- gotoxy(1, 1);
- clreol();
- printf("Demon Internet Simple News reindex utility\n");
- gotoxy(1, 2);
- clreol();
- printf(" Version %d.%02d [build %d]\n\n", rmj, rmm, rup);
- head = load_active_file();
- head = head; /* remove error message */
- #ifdef ATARI
- if (argc > 1 && (argv[1][0] == '/' || argv[1][0] == '-')) {
- #else
- if ((argc > 2) || (argv[1][0] == '/') || (argv[1][0] == '-')) {
- #endif
- printf("reindex: usage is reindex [newsgroup].\n");
- gotoxy(1, scr_rows - 1);
- exit(0);
- }
-
- if (argc == 2) {
- strcpy(newsgroup, argv[1]);
- printf("Processing newsgroup %s", newsgroup);
- }
- else {
- p = select_group(head, NULL);
- if (p == NULL) {
- printf("Reindex: Processing Terminated\n\n");
- gotoxy(1, scr_rows-1);
- exit(1);
- }
- else
- strcpy(newsgroup, p);
- }
- clrscr();
- gp = find_news_group(newsgroup, &junk_flag);
- if ((gp == NULL) || (junk_flag == 1)) {
- printf("reindex: cannot find newsgroup %s\n\n", newsgroup);
- gotoxy(1, scr_rows - 1);
- exit(1);
- }
-
- fname = make_news_group_name(gp->group);
- printf("Newsgroup %s selected\n\n", newsgroup);
- printf("article file identified as %s\n", fname);
- printf("index file identified as %s.idx\n\n", fname);
- printf("Proceed ? (y/n) ");
- ch = getch();
- putch(ch);
- ch = tolower(ch);
- if (ch != 'y') {
- printf("\nreindex: terminated.\n\n");
- gotoxy(1, scr_rows - 1);
- exit(0);
- }
-
- putch('\n');
- do_index(gp);
- do_active(gp);
- summary(gp);
- close_active_file();
- }
- else {
- #ifdef ATARI
- textcolor(textf); textbackground(textb);
- #else
- textcolor(7); textbackground(0);
- #endif
- clrscr();
- printf("reindex: Couldn't find necessary item in the .rc files.\n");
- }
-
- #ifdef ATARI
- textcolor(textf); textbackground(textb);
- printf("\033q");
- #else
- textcolor(7); textbackground(0);
- #endif
- gotoxy(1, scr_rows - 1);
- }
-
- void do_index(ACTIVE * gp)
- {
-
- FILE *afp, *ifp, *nfp;
- char *aname, iname[80], nname[80], bname[80], abuf[256], ibuf[256];
- char isubject[512], asubject[512];
- char *ind, *art, ch;
- unsigned long ioffset, inumber, itime;
- unsigned long aoffset, anext, anumber, atime;
- int flag, flag2, noquery;
-
- aname = make_news_group_name(gp->group);
- aoffset = 0l;
- flag2 = 0l;
- count = 0;
- noquery = FALSE;
- time(&t);
-
- strcpy(iname, aname);
- strcat(iname, ".idx");
- strcpy(nname, aname);
- strcat(nname, ".ndx");
- strcpy(bname, aname);
- strcat(bname, ".bak");
-
- if ((afp = fopen(aname, "rb")) == NULL) {
- printf("reindex: unable to open article file.\n\n");
- gotoxy(1, scr_rows - 1);
- exit(1);
- }
-
- if ((ifp = fopen(iname, "rb")) == NULL) {
- printf("reindex: unable to open index file.\n");
- printf("reindex: creating new index file.\n\n");
- }
-
- if ((nfp = fopen(nname, "wb")) == NULL) {
- printf("reindex: unable to open update file.\n\n");
- gotoxy(1, scr_rows - 1);
- exit(1);
- }
-
- do {
- strcpy(asubject, " (none)\n");
- flag = 0;
-
- do {
- art = fgets(abuf, 255, afp);
- if (strlen(abuf) == 1)
- flag = 1;
- if ((strnicmp(abuf, "Subject:", 8) == 0) && (flag == 0)) {
- if ((strlen(abuf) >= 137) && (abuf[136] != '\n')) {
- abuf[136] = '\n';
- abuf[137] = '\0';
- }
- strcpy(asubject, abuf + 8);
- }
- if (strnicmp(abuf, "@@@@END", 7) == 0) {
- anext = ftell(afp);
- break;
- }
- }
- while (art != NULL);
-
- if (art != NULL) {
- if (ifp != NULL)
- ind = fgets(ibuf, 255, ifp);
- else
- ind = NULL;
- gotoxy(1, 15);
- clreol();
- if (ind == NULL) {
- ioffset = inumber = itime = 0l;
- strcpy(isubject, "reindex: entry invalid");
- }
- else {
- ioffset = atol(ibuf);
- inumber = atol(ibuf + 9);
- itime = atol(ibuf + 18);
- strcpy(isubject, ibuf + 28);
- }
-
- cprintf("Ind> %08lu %08lu %09lu %-45.45s", ioffset, inumber, itime, isubject);
- if (flag2 == 0) {
- first = inumber - 1;
- anumber = inumber;
- num_art = 0;
- if (first == -1) {
- first = 0;
- anumber = 1;
- }
- atime = itime;
- flag2 = 1;
- }
- gotoxy(1, 17);
- clreol();
- if ((atime == 0l) || (atime < itime))
- atime = itime;
- cprintf("Art> %08lu %08lu %09lu %-45.45s", aoffset, anumber, atime, asubject);
- if ((ioffset != aoffset) || (inumber != anumber) ||
- (strcmp(isubject, asubject) != 0) ||
- (itime < atime)) {
- gotoxy(1, 19);
- if (noquery == FALSE) {
- printf("Index error in this article, correct it ? (y[es]/n[o]/d[on't ask]) ");
- ch = getch();
- putch(ch);
- }
- else {
- ch = 'y';
- }
- ch = tolower(ch);
- if (ch == 'd') {
- noquery = TRUE;
- ch = 'y';
- }
- if (ch == 'y') {
- count++;
- if (atime == 0l)
- atime = t;
- sprintf(ibuf, "%08lu %08lu %09lu %s", aoffset, anumber, atime, asubject);
- }
- gotoxy(1, 19);
- clreol();
- }
- if (fputs(ibuf, nfp) == EOF) {
- printf("reindex: index file write error.\n\n");
- gotoxy(1, scr_rows - 1);
- exit(2);
- }
-
- aoffset = anext;
- anumber++;
- }
- num_art++;
- }
- while (art != NULL);
-
- last = anumber;
- if (num_art < 2) {
- first = last = gp->hi_num;
- last++;
- }
- fclose(ifp);
- fclose(afp);
- fclose(nfp);
- if (count > 0) {
- unlink(bname);
- if (rename(iname, bname) != 0) {
- printf("reindex: Unable to rename old index file to .bak\n\n");
- }
- if (rename(nname, iname) != 0) {
- printf("reindex: unable to rename new index file to .idx\n\n");
- gotoxy(1, scr_rows - 1);
- exit(4);
- }
-
- }
- else {
- unlink(nname);
- }
- }
-
- void do_active(ACTIVE * gp)
- {
- --last;
- printf("Low number = %ld, High number = %ld\n\n", first, last);
- gp->lo_num = first;
- gp->hi_num = last;
- update_active_entry(gp);
- }
-
- void summary(ACTIVE * gp)
- {
-
- int i;
- char *name;
-
- for (i = 15; i < scr_rows; i++) {
- gotoxy(1, i);
- clreol();
- }
-
- gotoxy(1, 15);
- printf("reindex: processing completed.\n\n");
- if (count > 0) {
- printf("The following file has been modified:-\n");
- name = make_news_group_name(gp->group);
- printf(" %s.idx\n", name);
- printf("The original file (if found) has been renamed to:-\n");
- printf(" %s.bak\n", name);
- printf("%d changes were made to the index file.\n", count);
- printf("Active file updated.\n");
- }
- else {
- printf("No changes were made to the index file.\n");
- printf("Active file updated.\n");
- }
- }
-
- char *select_group(ACTIVE *head, ACTIVE *current)
- {
- ACTIVE *top; /* Newsgroup at page top */
- ACTIVE *this_group; /* Current Newsgroup */
- int exit_code; /* Why we are exiting */
- int ch, i;
-
- this_group = (current == NULL) ? head : current;
-
- top = head;
- exit_code = 0;
-
- textcolor(textf); textbackground(textb);
- clrscr();
- show_groups(&top, this_group, TRUE, head);
-
- while (exit_code == 0) {
-
- ch = getch();
- switch (ch) {
- case 0:
- case 0xE0:
-
- ch = getch();
- switch (ch) {
-
- case UP_ARR:
- if (this_group->last != NULL)
- this_group = this_group->last;
- break;
-
- case DN_ARR:
- if (this_group->next != NULL)
- this_group = this_group->next;
- break;
-
- case PGUP:
- for(i=0;i< PAGE_LENGTH; i++) {
- if (this_group->last == NULL) break;
- this_group = this_group->last;
- }
- break;
-
- case PGDN:
- for (i=0;i<PAGE_LENGTH;i++) {
- if (this_group->next == NULL) break;
- this_group = this_group->next;
- }
- break;
-
- case HOME:
- top = this_group = head;
- show_groups(&top, this_group, TRUE, head);
- break;
-
- case END:
- this_group = head;
- while (this_group->next != NULL)
- this_group = this_group->next;
- break;
-
- }
- break;
- case ENTER:
- exit_code = EX_DONE;
- break;
-
- case ESCAPE:
- case 'Q':
- case 'q':
- exit_code = EX_QUIT;
- break;
- }
- if (exit_code == 0)
- show_groups(&top, this_group, FALSE, head);
- }
- #ifdef ATARI
- textcolor(textf); textbackground(textb);
- #else
- textcolor(7); textbackground(0);
- #endif
- clrscr();
-
- if (exit_code == EX_DONE)
- return (this_group->group);
- else
- return ('\0');
- }
-
- void show_groups(ACTIVE ** top, ACTIVE * this_group, int force, ACTIVE *head)
- {
-
- static last_y;
- static last_index;
- int i;
- ACTIVE *that;
-
- if (force || ((*top)->index > this_group->index) ||
- (this_group->index - (*top)->index) > PAGE_LENGTH-1) {
-
- clrscr();
- textbackground(headb); textcolor(headf);
- #ifdef ATARI
- printf("%*s", scr_cols, " ");
- printf("%*s", scr_cols, " ");
- gotoxy(1, 1);
- #else
- clreol();
- #endif
- cprintf(" Select Newsgroup (%s) [%ldk] \r\n",
- VERSION, farcoreleft()/1024);
- #ifndef ATARI
- clreol();
- #else
- gotoxy(1, 2);
- #endif
- cprintf(" Reindex");
-
- gotoxy(60,2);
- cprintf("Group %3d of %3d\r\n", last_index+1, head->groups);
- textbackground(textb); textcolor(textf);
-
- /* now adjust the top */
- *top = this_group;
- for (i=0;i<PAGE_LENGTH/2;i++) {
- if ((*top)->last == NULL) break;
- *top = (*top)->last;
- }
-
- that = *top;
-
- for (i = 0; i < PAGE_LENGTH; i++) {
- gotoxy(7, i + 4);
- printf(" %4ld %s", that->hi_num - that->lo_num,
- that->group);
- that = that->next;
- if (that == NULL)
- break;
- }
-
- last_y = this_group->index - (*top)->index;
- gotoxy(5, last_y + PAGE_HEADER);
- putch('-');
- putch('>');
- last_index = this_group->index;
- }
- else {
-
- gotoxy(5, last_y + PAGE_HEADER);
- putch(' '); putch(' ');
-
- last_y += (this_group->index - last_index);
- gotoxy(5, last_y + PAGE_HEADER);
- putch('-');
- putch('>');
- last_index = this_group->index;
- }
-
- gotoxy(60,2);
- textbackground(headb); textcolor(headf);
- cprintf("Group %3d of %3d\r\n", last_index+1, head->groups);
- textbackground(textb); textcolor(textf);
-
- command("ESC=quit ENTER=select group");
-
- }
-
- void command(char *msg)
- {
- int x;
-
- gotoxy(1, scr_rows);
- textbackground(msgb);
- textcolor(msgf);
- clreol();
-
- x = (scr_cols - strlen(msg)) / 2;
- gotoxy(x, scr_rows);
- cprintf(msg);
-
- textbackground(textb);
- textcolor(textf);
- }
-
- #ifndef __TURBOC__
- #ifndef ATARI
-
- unsigned long farcoreleft(void)
- {
-
- union _REGS inregs, outregs;
-
- _heapmin();
- inregs.h.ah = 0x48;
- inregs.x.bx = 0xffff;
- _intdos(&inregs, &outregs);
-
- return (16l * (long)outregs.x.bx);
- }
-
- #endif
- #endif
-